
from copy import copy

from PySide2 import QtCore
from PySide2.QtGui import QColor,QPalette,QShowEvent,QIcon
from PySide2.QtWidgets import QApplication,QDialog,QWidget,QColorDialog

from UI.ui_changecolordialog import Ui_ChangeColorDialog

class ChangeColorDialog(QDialog):

    def __init__(self, parent: QWidget=None) -> None:
        super(ChangeColorDialog,self).__init__(parent)

        self.ui=Ui_ChangeColorDialog()
        self.ui.setupUi(self)

        self.__InitData()
        self.__InitUI()
        self.__InitSignalAndSlot()


    def showEvent(self, event: QShowEvent) -> None:
        super().showEvent(event)
        self.backColor=copy(ChangeColorDialog.Color)
        self.SetColorPreview(ChangeColorDialog.Color)
        self.SetSliderValue(ChangeColorDialog.Color)

    def reject(self) -> None:
        super().reject()
        ChangeColorDialog.Color=self.backColor

    def __InitData(self):
        pass

    def __InitUI(self):
        self.setWindowIcon(QIcon(":/Image/UsageTool.png"))
        ScreenSize= QApplication.primaryScreen().size()
        self.move(ScreenSize.width()/2-self.size().width()/2,ScreenSize.height()/2-self.size().height()/2)

    def __InitSignalAndSlot(self):
        self.ui.horizontalSlider_ColorR.valueChanged.connect(lambda: self.slotSliderValueChange("R"))
        self.ui.horizontalSlider_ColorG.valueChanged.connect(lambda: self.slotSliderValueChange("G"))
        self.ui.horizontalSlider_ColorB.valueChanged.connect(lambda: self.slotSliderValueChange("B"))
        self.ui.horizontalSlider_ColorA.valueChanged.connect(lambda: self.slotSliderValueChange("A"))

        self.ui.pushButton_SetDefault.clicked.connect(self.slotSetDefaultColor)
        self.ui.pushButton_Color_Preview.clicked.connect(self.slotColorPreviewClicked)

        self.ui.buttonBox.accepted.connect(self.accept)
        self.ui.buttonBox.rejected.connect(self.reject)
        
    def slotSliderValueChange(self,Role:str):

        if Role=="R":
            ChangeColorDialog.Color.setRed(self.ui.horizontalSlider_ColorR.value())
        elif Role=="G":
            ChangeColorDialog.Color.setGreen(self.ui.horizontalSlider_ColorG.value())
        elif Role=="B":
            ChangeColorDialog.Color.setBlue(self.ui.horizontalSlider_ColorB.value())
        elif Role=="A":
            ChangeColorDialog.Color.setAlpha(self.ui.horizontalSlider_ColorA.value())

        self.SetColorPreview(ChangeColorDialog.Color)
        self.signalColorChanged.emit()

    def slotSetDefaultColor(self):
        ChangeColorDialog.Color=QColor(200,230,255,220)
        self.SetColorPreview(ChangeColorDialog.Color)
        self.SetSliderValue(ChangeColorDialog.Color)
        self.signalColorChanged.emit()

    def slotColorPreviewClicked(self):

        ColorDialog = QColorDialog(self)
        ColorDialog.setOption(QColorDialog.ShowAlphaChannel, True)
        ColorDialog.setCurrentColor(self.Color)
        if ColorDialog.exec_() == QDialog.Accepted:
            color = ColorDialog.currentColor()
            self.SetSliderValue(color)



    def SetColorPreview(self,Color:QColor):
        palette= self.ui.pushButton_Color_Preview.palette()
        palette.setColor(QPalette.Button, Color)
        self.ui.pushButton_Color_Preview.setPalette(palette)

    def SetSliderValue(self,Color:QColor):
        self.ui.horizontalSlider_ColorR.setValue(Color.red())
        self.ui.horizontalSlider_ColorG.setValue(Color.green())
        self.ui.horizontalSlider_ColorB.setValue(Color.blue())
        self.ui.horizontalSlider_ColorA.setValue(Color.alpha())


    Color=QColor()

    signalColorChanged=QtCore.Signal()




        